Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 05-Oct-2010 (Priyabrat)

Public Class frmUrineTest
    Dim mstrMode As String
    Public blnAdd As Boolean, blnEdit As Boolean, blnDelete As Boolean, blnLocked As Boolean
    Dim mlngRecordID As Long
    Dim blnUnload As Boolean
    Dim mblnChanged As Boolean
    Dim mblnActivate As Boolean
    Dim mblnExpanded As Boolean = False
    Dim mblnAutoSize As Boolean = True
    Dim gridView1 As Views.Grid.GridView
    Dim mblnCancelDisplay As Boolean
    Dim IsExitOnSave As Boolean
    Dim VisitId As Integer
    Public mblnCheck As Boolean = False
    Public mintSenderID As Integer

    Public Sub New(ByVal id As Integer, Optional ByVal ExitOnSave As Boolean = True)

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        VisitId = id
        IsExitOnSave = ExitOnSave
    End Sub

    Private Sub frmMessages_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            mblnCheck = False
            Dim intUnload As DialogResult

            If mblnChanged = True And mstrMode <> "" Then
                intUnload = MessageBox.Show("You have not saved your changes to the record. Do you want to save your changes now?", gstrMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                If intUnload = Windows.Forms.DialogResult.Cancel Then
                    e.Cancel = True : Exit Sub
                ElseIf intUnload = Windows.Forms.DialogResult.Yes Then
                    blnUnload = True
                    If blnSaveRecords() = False Then
                        e.Cancel = True
                        blnUnload = False
                        Exit Sub
                    End If
                    mblnChanged = False
                End If
            End If
            blnUnload = False

        Catch ex As Exception
            blnUnload = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub frmUrineTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Call SetImageControl(Me)
            Me.Icon = System.Drawing.Icon.FromHandle(CType(mdiMain.imgFormIcons.Images(2), Bitmap).GetHicon())
            Call GetAccessPermission(Me, "frmUrineTest")
            mblnActivate = True
            mblnChanged = False
            Call DispalyData()
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub DispalyData()
        Try
            'Urine Sample
            Dim strQuery As String = "SELECT * FROM tblVisitUrine WHERE VisitId= " & VisitId
            Dim dsTemp As DataSet = FillDataSet(strQuery)
            mstrMode = "Add"
            With dsTemp.Tables(0)
                If .Rows.Count > 0 Then
                    cboBlood.Text = .Rows(0).Item("Blood")
                    cboColor.Text = .Rows(0).Item("Colour")
                    cboketone.Text = .Rows(0).Item("Ketone")
                    cboProtein.Text = .Rows(0).Item("Protein")
                    cboSugar.Text = .Rows(0).Item("Sugar")
                    txtSpGravity.EditValue = .Rows(0).Item("SpGravity") & ""
                    mstrMode = "Edit"
                End If
            End With

            dsTemp.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Public Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If blnSaveRecords() = False Then Exit Sub

        If IsExitOnSave Then
            Me.Close()
        End If

    End Sub

    Private Function blnSaveRecords() As Boolean
        blnSaveRecords = False
        Dim blnIsTransactionOpen As Boolean
        Dim strQuery As String, strFeilds As String
        Try
            gtrnWebPlusMed = gconWebPlusMed.BeginTransaction
            blnIsTransactionOpen = True
            Dim intCtr As Integer = 0

            If mstrMode = "Add" Then
                strFeilds = " tblVisitUrine(VisitId,Colour,Protein,Sugar,Ketone,Blood,SpGravity)"
                strQuery = VisitId & ",'" & cboColor.Text & "','" & cboProtein.Text & "','" & cboSugar.Text & "','" & cboketone.Text & "','" & cboBlood.Text & "','" & txtSpGravity.Text & "'"

                Call InsertintoTable(strFeilds, strQuery)
            Else
                strQuery = "VisitId=" & VisitId & ",Colour='" & cboColor.Text & "',Protein='" & cboProtein.Text & "',Sugar='" & cboSugar.Text & "',Ketone='" & cboketone.Text & "',Blood='" & cboBlood.Text & "',SpGravity='" & txtSpGravity.Text & "'"

                Call UpdateTable("tblVisitUrine", strQuery, " VisitId=" & VisitId)
            End If

            gtrnWebPlusMed.Commit()
            blnIsTransactionOpen = False
            blnSaveRecords = True
            mblnChanged = False
        Catch ex As Exception
            If blnIsTransactionOpen = True Then
                gtrnWebPlusMed.Rollback()
            End If
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Function

    Private Sub cboColor_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSpGravity.EditValueChanged, cboSugar.EditValueChanged, cboProtein.EditValueChanged, cboketone.EditValueChanged, cboColor.EditValueChanged, cboBlood.EditValueChanged
        mblnChanged = True
    End Sub
End Class
